<?php
namespace app\api\controller;



use app\common\model\tang\MerchantEmployees;

class Employees extends Base
{
    /**
     * 获取员工列表
     */
    public function getEmployeesList(){
        $list = (new MerchantEmployees)->where('merchant_id',$this->merchant_id)->whereIn('status',[0,1])->order('id desc')->select();
        if($list->isEmpty()){
            return SendSuccess('success',[]);
        }else{
            $list = $list->toArray();
            foreach ($list as $k=>$v){
                $list[$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
            }
            return SendSuccess('success',$list);
        }
    }
    /**
     * 员工获取自己的工作牌
     */
    public function getUserbadge(){
        $result= (new MerchantEmployees())->with('merchant')->where('merchant_id',$this->merchant_id)->where('mobile',$this->mobile)->find();
        if(empty($result)){
            return SendSuccess('success',[]);
        }else{
            $result['create_time'] = date('Y-m-d H:i:s',$result['create_time']);
            return SendSuccess('success',$result);
        }
    }
    /**
     * 员工离职
     */
    public function EmployeeEeparture(){
        $id = $this->request->param('id/d');
        $result= (new MerchantEmployees())->where('id',$id)->where('status',1)->find();
        if(empty($result)){
            return SendError('员工不存在或已离职');
        }else{
            (new MerchantEmployees())->update([
                'status'=>-1
            ],['id'=>$id]);
            return SendSuccess('success',[]);
        }
    }
    /**
     * 商家获取员工工牌
     */
    public function getBadge(){
        $id = $this->request->param('id');
        if(empty($id)){
            return SendError('请传入员工id');
        }
        $result= (new MerchantEmployees())->with('merchant')->where('id',$id)->find();
        if($result->isEmpty()){
            return SendSuccess('success',[]);
        }else{
            $result['create_time'] = date('Y-m-d H:i:s',$result['create_time']);
            return SendSuccess('success',$result);
        }
    }
    /**
     * 获取员工信息
     */
    public function getBadgeInfo(){
        $id = $this->request->param('id');
        if(empty($id)){ //说明是自己修改
            $isFirst = (new MerchantEmployees)->with('merchant')->where(['mobile' => $this->mobile])->find();
            if(empty($isFirst)){
                return SendError('编辑的员工不存在');
            }
            return  SendSuccess('success',$isFirst);

        }else{//大账号修改
            $isFirst = (new MerchantEmployees)->with('merchant')->where(['id' => $id])->find();
            if(empty($isFirst)){
                return SendError('编辑的员工不存在');
            }
            return  SendSuccess('success',$isFirst);
        }
    }
    /**
     * 编辑员工信息
     */
    public function updateInfo(){
        $param = $this->request->param();
        $isFirst = (new MerchantEmployees)->where(['id' => $param['id']])->find();
        if (empty($isFirst)) {
            return SendError('该员工不存在');
        }
        $id = $param['id'];
        unset($param['id']);
        (new MerchantEmployees)->where('id',$id)->save($param);
        return  SendSuccess('编辑成功',[]);
    }
}